/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.is.city.search;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;

/**
 *
 * @author oleg
 */
public class SearchSettingsController {

    String jdbc_url;
    Properties props; //свойства для соединения с БД
    Preferences prefs=null;
    
    public SearchSettingsController() {
        init();
        try {
            Connection conn = DriverManager.getConnection(jdbc_url, props);
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM shops");
            while (rs.next()) {
                System.out.print("Column 1 returned ");
                System.out.println(rs.getString(1));
            }
            rs.close();
            st.close();
            conn.close();

        } catch (SQLException ex) {
            Logger.getLogger(SearchEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void init() {
        
        try {
            FileInputStream is = new FileInputStream(this.getClass().getName());
            Preferences.importPreferences(is);
            is.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SearchEngine.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Файл настроек не существует, но будет создан.");
        } catch (IOException ex) {
            Logger.getLogger(SearchEngine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidPreferencesFormatException ex) {
            Logger.getLogger(SearchEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
        prefs = Preferences.userRoot().node(this.getClass().getName());
        String address = "Test1";
        System.out.println(prefs.get(address, "Hello world"));

        prefs.put(address, "Hello Mosco");
        System.out.println(prefs.get(address, "no value &"));
/*
        prefs.put("jdbc_url", "jdbc:postgresql://localhost/gis");
        prefs.put("jdbc_user", "oleg");
        prefs.put("jdbc_password", "");
        prefs.put("jdbc_ssl", "false");
*/
        jdbc_url = prefs.get("jdbc_url", "");
        props = new Properties();
        props.setProperty("user", prefs.get("jdbc_user", "empty"));
        props.setProperty("password", prefs.get("jdbc_password", "empty"));
        save();
    }

    public String getJdbc_url() {
        return jdbc_url;
    }

    public Properties getProperties(){
        return props;
    }

    public void setJdbc_url(String jdbc_url) {
        this.jdbc_url = jdbc_url;
        prefs.put("jdbc_url", jdbc_url);
    }

    public void setProps(Properties props) {
        this.props = props;
        prefs.put("jdbc_user", props.getProperty("user"));
        prefs.put("jdbc_password", props.getProperty("password"));
    }

    public void save(){
        FileOutputStream outputStream=null;
        try {
            outputStream = new FileOutputStream(this.getClass().getName());
            prefs.exportSubtree(outputStream);
            outputStream.close();
        } catch (BackingStoreException ex) {
            Logger.getLogger(SearchEngine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SearchEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
